#
# FanPengfei,  2019/04/22
# 
# All rights reserved.
#./make_build_wafsdk.sh Release Revision
#./make_build_wafsdk.sh Beta Revision

#!/bin/bash
MAJOR_VER=2
MINOR_VER=3
REV_VER=105

BUILD_ROOT=./wafsdk_buildroot
GIT_REPO="http://git.jd.com/jsec-s-plane/vpcwaf-sdk.git --recursive"
PUBDIR=pub_wafsdk

MY_PATH=${PWD}/$0

if [ $# != 2 ] || ([ $1 != "Alpha" ] && [ $1 != "Beta" ] && [ $1 != "Release" ]) || ([ $2 != "Major" ] && [ $2 != "Minor" ] && [ $2 != "Revision" ]); then
	echo "Usage: $0 <Alpha | Beta | Release> <Major | Minor | Revision>"
	exit -1;
fi

PHASE=$1
INC_VERSION=$2

# Update new version
if [ $INC_VERSION == "Major" ]; then
	let MAJOR_VER++;
	MINOR_VER=0;
	REV_VER=0;
elif [ $INC_VERSION == "Minor" ]; then
	let MINOR_VER++;
	REV_VER=0;
elif [ $INC_VERSION == "Revision" ]; then
	let REV_VER++;
fi

if [ -d ${BUILD_ROOT} ]; then
	rm -rf ${BUILD_ROOT}
fi

mkdir ${BUILD_ROOT}/vpcwaf-sdk -p 

git clone ${GIT_REPO} ${BUILD_ROOT}/vpcwaf-sdk

cd ${BUILD_ROOT}

cd vpcwaf-sdk/
git checkout -b vpcwaf2.3 origin/vpcwaf2.3 
sh build.sh ${PHASE} ${MAJOR_VER}.${MINOR_VER}.${REV_VER}
echo "sh build.sh ${PHASE} ${MAJOR_VER}.${MINOR_VER}.${REV_VER}"

RPM_LIST=`ls *.tar.gz`

#return BUILD_ROOT
cd - 

if [ $? == "0" ]; then
	sed -i "s/^MAJOR_VER=.*/MAJOR_VER=$MAJOR_VER/" $MY_PATH
	sed -i "s/^MINOR_VER=.*/MINOR_VER=$MINOR_VER/" $MY_PATH
	sed -i "s/^REV_VER=.*/REV_VER=$REV_VER/" $MY_PATH
else
	echo "Make build error !"
	exit -1;
fi

#Update publish web page
mv vpcwaf-sdk/*.tar.gz ../$PUBDIR/build

#cd ../$PUBDIR/build/
#cd - 

BUILD_DATE=`date -d today +"%Y-%m-%d %T"`

cd ./vpcwaf-sdk
GIT_SHA=`git log | head -n 1 | awk '{print $2}'`
cd -

if [ $PHASE == "Beta" ]; then
	TARGET_LINK_FILE=../$PUBDIR/beta_link.html
elif [ $PHASE == "Release" ]; then
	TARGET_LINK_FILE=../$PUBDIR/release_link.html
fi 

echo "          <h4>${BUILD_DATE} (Git sha: $GIT_SHA)</h4> "> ../$PUBDIR/tmp_link.html
for i in $RPM_LIST;  
do   
echo "          <a href="/build/$i">$i</a><br>" >> ../$PUBDIR/tmp_link.html
done   

if [ -f $TARGET_LINK_FILE ]; then
	cat $TARGET_LINK_FILE >> ../$PUBDIR/tmp_link.html
fi

mv -f ../$PUBDIR/tmp_link.html $TARGET_LINK_FILE

echo "<html>" > ../$PUBDIR/index.html
echo "      <head><title>WAFSDK build download</title></head>" >> ../$PUBDIR/index.html
echo "      <body>" >> ../$PUBDIR/index.html
echo "          <h1>WAFSDK build download</h1> " >> ../$PUBDIR/index.html
if [ -f ../$PUBDIR/release_link.html ]; then
echo "          <h3 style=\"color:red;font-size:20px;\">Release build:</h3>" >> ../$PUBDIR/index.html
	cat ../$PUBDIR/release_link.html >> ../$PUBDIR/index.html
fi

if [ -f ../$PUBDIR/beta_link.html ]; then
echo "          <h3 style=\"color:red;font-size:20px;\">Beta build:</h3>" >> ../$PUBDIR/index.html
	cat ../$PUBDIR/beta_link.html >> ../$PUBDIR/index.html
fi
echo "      </body>" >> ../$PUBDIR/index.html
echo "</html>" >> ../$PUBDIR/index.html
